package vnu.edu;

import jade.core.AID;
import jade.core.Agent;
import jade.core.PlatformID;
import jade.core.behaviours.TickerBehaviour;

public class MobileAgent extends Agent {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void setup() {
		super.setup();
		addBehaviour(new MyTickerBehaviour(this, 1000));
		System.out.println("Hello World. I am an agent!");
		System.out.println("My LocalName: " + getAID().getLocalName());
		System.out.println("My Name: " + getAID().getName());
		System.out.println("My Address: " + getAID().getAddressesArray()[0]);
	}

	private class MyTickerBehaviour extends TickerBehaviour {
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		Agent agent;
		// long interval;
		int counter;

		public MyTickerBehaviour(Agent agent, long interval) {
			super(agent, interval);
			this.agent = agent;
			counter = 0;
		}

		@Override
		protected void onTick() {
			if (counter == 3) {
				// move out
				AID remoteAMS = new AID("amm@iNIL", AID.ISGUID);
				remoteAMS.addAddresses("http://iNIL:7778/acc");
				PlatformID destination = new PlatformID(remoteAMS);
				agent.doMove(destination);
			}
			if (counter <= 10)
				System.out.println(counter++);
			else
				agent.doDelete();
		}

	}
}